141. 环形链表
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) 
    {
        if(head==NULL)
        {
            return false;
        }
        ListNode * faster = head->next;
        ListNode * slower = head;

        while(faster!=NULL)
        {
            if(faster!=NULL&&slower!=NULL&&faster==slower)
            {
                return true;
            }

            slower=slower->next;
            faster=faster->next;
            
            if(faster!=NULL)
            {
                faster=faster->next;
            }
        }

        return false;    
    }
};


2. 两数相加
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) 
    {
        int co = 0;
        int sum = 0;
        ListNode* ret = nullptr;
        ListNode* tail = ret;

        ListNode* adder1 = l1;
        ListNode* adder2 = l2;

        while(adder1!=nullptr&&adder2!=nullptr)
        {
            sum = co + adder1->val + adder2->val;
            co = sum/10;
            sum %= 10;

            if(ret == nullptr)
            {
                ret = new ListNode(sum);
                tail = ret;
            }
            else 
            {
                tail->next = new ListNode(sum);
                tail = tail->next;
            }

            adder1=adder1->next;
            adder2=adder2->next;
        }    

        while(adder1!=nullptr)
        {
            sum = co + adder1->val;
            co = sum/10;
            sum %= 10;

            if(ret == nullptr)
            {
                ret = new ListNode(sum);
                tail = ret;
            }
            else 
            {
                tail->next = new ListNode(sum);
                tail = tail->next;
            }

            adder1=adder1->next;
        }

        while(adder2!=nullptr)
        {
            sum = co + adder2->val;
            co = sum/10;
            sum %= 10;

            if(ret == nullptr)
            {
                ret = new ListNode(sum);
                tail = ret;
            }
            else 
            {
                tail->next = new ListNode(sum);
                tail = tail->next;
            }

            adder2=adder2->next;
        }

        if(co!=0)
        {
            tail->next = new ListNode(co);
        }
        return ret;
    }
};


1751. 最多可以参加的会议数目 II
class Solution {
public:
    int maxValue(vector<vector<int>>& events, int k) 
    {
        int n = events.size();
        vector<vector<int>> dp(n+1,vector<int>(k+1,0));
        ranges::sort(events.begin(),events.end(),[](vector<int>&e1,vector<int>&e2){ return e1[1] < e2[1];});    

        for(int i=0;i<events.size();i++)
        {
            int p = ranges::lower_bound(events,events[i][0],{},[](const auto&e){ return e[1];}) - events.begin();
            for(int j=1;j<=k;j++)
            {   
                dp[i+1][j] = max(dp[i][j],dp[p][j-1]+events[i][2]);
            }
        }

        return dp[n][k];
    }
};